package com.sprucetec.live.common.webcontext;

import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.sprucetec.live.base.entity.User;
import com.sprucetec.live.constants.SprucetecConstant;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyb
 * Date: 2019/1/17
 * Time: 16:04
 * Description: 从当前线程中获取登录用户 通过dubbo 传递到service层
 **/
public class LoginControllerDubboFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        User user = LoginUserContext.getUser();
        if (user != null) {
            RpcContext.getContext().setAttachment(SprucetecConstant.DUBBO_LOGIN_USER_CODE, user.getUid());
            RpcContext.getContext().setAttachment(SprucetecConstant.DUBBO_LOGIN_USER_NAME, user.getName());
            RpcContext.getContext().setAttachment(SprucetecConstant.DUBBO_LOGIN_USER_EMAIL, user.getEmail());
            RpcContext.getContext().setAttachment(SprucetecConstant.DUBBO_LOGIN_USER_EMAIL_PREFIX, user.getEmailPrefix());
            RpcContext.getContext().setAttachment(SprucetecConstant.DUBBO_LOGIN_USER_ROLE, user.getRoles());
        }
        return invoker.invoke(invocation);
    }
}
